#!/usr/bin/env ruby

# Based on https://github.com/asciidoctor/asciidoctor-extensions-lab/blob/master/scripts/asciidoc-coalescer.rb

require 'asciidoctor'
require 'optparse'

options = {}
OptionParser.new do |o|
  o.banner = 'Usage: ruby coalesce-readme.rb -i <input file> -o <output file>'
  o.on('-i', '--input FILE', 'Input file') do |input|
    options[:input] = input.strip
  end
  o.on('-o', '--output FILE', 'Write output to FILE instead of stdout.') do |output|
    options[:output] = output.strip unless output.strip == '-'
  end
end.parse!

doc = Asciidoctor.load_file options[:input],
                            safe:       :unsafe,
                            parse:      false,
                            attributes: 'allow-uri-read'

out = <<-EOF.gsub(/^\s+/, '')
  // Do not edit this file - it's automatically generated
  :toc:
EOF

out << doc.reader.read

if options[:output]
  File.open(options[:output], 'w+') do |f|
    f.write(out)
  end
else
  puts out
end
